package com.crm.system.controller.user;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.dto.MainSalesmanDto;
import com.crm.model.dto.SalesmanDto;
import com.crm.model.entity.sys.SalesmanSupportLog;
import com.crm.system.service.user.MainSalesmanService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yun_cheng
 * @date 2021年12月22日 上午 11:37:49
 * @description 首页销售明星榜
 */
@RestController
@RequestMapping("/mainSalesman")
@CrossOrigin(origins = "*", maxAge = 3600)
@Slf4j
public class MainSalesmanController {

    @Autowired
    private MainSalesmanService mainSalesmanService;

    /**
     * 新增支持信息
     * @return
     */
    @PostMapping("/addSalesmanLog")
    public DataResponse addSalesmanLog(@RequestBody SalesmanSupportLog salesmanSupportLog) {
        try {
            String message = mainSalesmanService.addSalesmanLog(salesmanSupportLog);
            if ("SUCCESS".equals(message)) {
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
            } else if ("FAIL".equals(message)) {
                return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            } else {
                return new DhResponse(ResponseCodeEnum.BUSINESS_ERROR.getResponseCode(), message);
            }
        } catch (Exception e) {
            log.error("MainSalesmanController getTopSalesman error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 查询个人业绩排行榜（redis）和点赞、送花、留言统计
     *
     * @param date 查询年月 YYYY-MM
     * @return
     */
    @GetMapping("/getSalesmanGiftStat")
    public DataResponse getSalesmanGiftStat(@RequestParam String date) {
        try {
            List<MainSalesmanDto> salesmanScoreMoneyList = mainSalesmanService.getSalesmanGiftStat(date);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), salesmanScoreMoneyList);
        } catch (Exception e) {
            log.error("MainSalesmanController getTopSalesman error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 查询个人业绩前三的留言榜信息
     *
     * @param date 查询年月 YYYY-MM
     * @return
     */
    @GetMapping("/getTopSalesmanMessage")
    public DataResponse getTopSalesmanMessage(@RequestParam Integer pageIndex,
                                              @RequestParam Integer pageSize) {
        try {
            PageInfo<Map<String, Object>> salesmanMessageList =
                    mainSalesmanService.getTopSalesmanMessage(pageIndex, pageSize);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), salesmanMessageList);
        } catch (Exception e) {
            log.error("MainSalesmanController getTopSalesman error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

}
